
package examplelesson2;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ListUsers {
    static int count = 0;
   
    public static void main(String[] args) {
        try {
            File f = new File("tomcat-users.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(f);
            printUserName(doc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
        private static void printUserName(Node node)
        {
            if (node == null) {
                return;
            }
            if (node.getNodeName().equals("user")) {
                count++;
                String str = node.getAttributes().getNamedItem("username").getNodeValue();
                System.out.println("User" + count + ": " + str);
            }
            NodeList children = node.getChildNodes();
            int i = 0;
            while (i < children.getLength()) {
                printUserName(children.item(i++));
            }
        }
    

}
